package com.ext.aic.apis.user;

import com.ext.aic.apis.BaseUrlConfig;
import com.ext.aic.beans.params.LoginParams;
import com.ext.aic.beans.response.LoginItem;
import com.eyinfo.android_pure_utils.events.Action0;
import com.eyinfo.android_pure_utils.events.Func2;

import java.util.HashMap;

import io.github.eyinfo.okrx.BaseOkRxService;
import io.github.eyinfo.okrx.BaseSubscriber;
import io.github.eyinfo.okrx.annotations.APIUrlInterfaceClass;
import io.github.eyinfo.okrx.annotations.ApiCheckAnnotation;
import io.github.eyinfo.okrx.beans.RetrofitParams;
import io.github.eyinfo.okrx.enums.DataType;
import io.github.eyinfo.okrx.enums.ErrorType;
import io.github.eyinfo.okrx.events.OnSuccessfulListener;
import lombok.Getter;
import lombok.Setter;

@APIUrlInterfaceClass(BaseUrlConfig.class)
public class LoginService<Extra> extends BaseOkRxService {

    @Getter
    @Setter
    private Action0 successCall;

    protected void onLoginFail(LoginParams loginParams, Extra extra) {

    }

    protected void onLoginSuccess(LoginItem loginResponse, LoginParams loginParams, Extra extra) {

    }

    @ApiCheckAnnotation
    public void submitLogin(LoginParams params, Extra extra) {
        BaseSubscriber<LoginItem, LoginService> subscriber = new BaseSubscriber<>(this);
        subscriber.setOnSuccessfulListener(new OnSuccessfulListener<LoginItem>() {
            @Override
            public void onSuccessful(LoginItem response, DataType dataType, Object... extras) {
                onLoginSuccess(response, params, extra);
            }

            @Override
            public void onError(ErrorType errorType, Object... extras) {
                onLoginFail(params, extra);
            }
        });
        requestObject(IUserAPI.class, this, subscriber, new Func2<RetrofitParams, IUserAPI, HashMap<String, Object>>() {
            @Override
            public RetrofitParams call(IUserAPI userAPI, HashMap<String, Object> stringObjectHashMap) {
                return userAPI.submitLogin();
            }
        });
    }
}
